﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Wintellect.PowerCollections;

class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T>
{
    private OrderedBag<T> priorityQueue;

    public PriorityQueue(PriorityQueue<T> priorityQueue)
    {
        this.priorityQueue = new OrderedBag<T>();
        if (priorityQueue != null)
        {
            foreach (var elem in priorityQueue)
            {
                this.priorityQueue.Add(elem);
            }
        }
    }
    public PriorityQueue()
        : this(null)
    { }

    public bool Enqueue(T elem)
    {
        this.priorityQueue.Add(elem);
        return true;
    }
    public T Dequeue()
    {
        if (priorityQueue.Count > 0)
        {
            return this.priorityQueue.RemoveFirst();
        }
        else
        {
            throw new InvalidOperationException("The queue is empty.");
        }
    }
    public T Peek()
    {
        if (priorityQueue.Count > 0)
        {
            return this.priorityQueue.GetFirst();
        }
        else
        {
            throw new InvalidOperationException("The queue is empty.");
        }
    }

    public IEnumerator<T> GetEnumerator()
    {
        foreach (T elem in this.priorityQueue)
        {
            yield return elem;
        }
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable<T>)this).GetEnumerator();
    }
}
